(function() {
    angular.module('app').component('attrBroadComponent', {
        templateUrl: 'app/controllers/attr.controller/attr.broad.component/attr.broad.component.html',
        controller: attrBroadComponent,
        controllerAs: 'vm',
        bindings: {
            filter: '='
        }
    });

    attrBroadComponent.$inject = ['$scope', 'attrBroadComponentService', '$loading', '$timeout','$rootScope'];

    function attrBroadComponent($scope, attrBroadComponentService, $loading, $timeout,$rootScope) {

        var vm = this;

        // console.log('--------------------attrBroadComponent.js loading-------------');
        // bind vm.filter;

        vm.table = [];

        vm.index = -1;

        vm.watchCombines = watchCombines; //function

        var clearFun = $rootScope.$on('tool-search-component-emit-EV',function(e,filter){

            // console.log(filter);

            init(filter);
        });
        

        $scope.$on('$destroy', function() {
            clearFun();
        });

        function init(filter) {

            // console.log('attrBroadComponent 开始区');

            $loading.start('attrBroadTable');            

            if (filter.category == "") {
                // console.log(json);
                return;

            }
            attrBroadComponentService.get(filter).then(function(data) {

                vm.table.length = 0;
                // vm.index = -1;
                vm.table = data;

                $timeout(function(){
                	if(vm.index !=-1){
                		vm.watchCombines(vm.table[vm.index], vm.index);
                	}else{
                		vm.watchCombines(vm.table[0], 0);
                	}
                	
                },500);

                $loading.finish('attrBroadTable');

            }, function(err) {

                
                vm.table.length = 0;
                $loading.finish('attrBroadTable');
            });
        }

        function watchCombines(item, $index) {
            vm.index = $index;
            var property = item.title;
      
            // console.log('-----------------------emit_attr_pool_component_EV----------------------');
            $rootScope.$emit('emit_attr_pool_component_EV',property);
        }

    }
})();
